
<!DOCTYPE html>

<html>
  
<!-- Mirrored from docs.sympy.org/latest/modules/utilities/decorator.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:11 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=utf-8" /><!-- /Added by HTTrack -->
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>Decorator &#8212; SymPy 1.9 documentation</title>
    <link rel="stylesheet" type="text/css" href="../../_static/pygments.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/default.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/graphviz.css" />
    <link rel="stylesheet" type="text/css" href="../../_static/plot_directive.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-core.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-autocomplete.css" />
    <link rel="stylesheet" type="text/css" href="../../../../live.sympy.org/static/live-sphinx.css" />
    
    <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
    <script src="../../_static/jquery.js"></script>
    <script src="../../_static/underscore.js"></script>
    <script src="../../_static/doctools.js"></script>
    <script src="../../../../live.sympy.org/static/utilities.js"></script>
    <script src="../../../../live.sympy.org/static/external/classy.js"></script>
    <script src="../../../../live.sympy.org/static/live-core.js"></script>
    <script src="../../../../live.sympy.org/static/live-autocomplete.js"></script>
    <script src="../../../../live.sympy.org/static/live-sphinx.js"></script>
    <script async="async" src="../../../../cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest8331.js?config=TeX-AMS_HTML-full"></script>
    <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"inlineMath": [["\\(", "\\)"]], "displayMath": [["\\[", "\\]"]]}})</script>
    
    <link rel="shortcut icon" href="../../_static/sympy-notailtext-favicon.ico"/>
    <link href="decorator.html" rel="canonical" />
    
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" />
    <link rel="next" title="Enumerative" href="enumerative.html" />
    <link rel="prev" title="Codegen" href="codegen.html" /> 
  </head><body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="enumerative.html" title="Enumerative"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="codegen.html" title="Codegen"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" accesskey="U">Utilities</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Decorator</a></li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-sympy.utilities.decorator">
<span id="decorator"></span><h1>Decorator<a class="headerlink" href="#module-sympy.utilities.decorator" title="Permalink to this headline">¶</a></h1>
<p>Useful utility decorators.</p>
<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.decorator.conserve_mpmath_dps">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.decorator.</span></span><span class="sig-name descname"><span class="pre">conserve_mpmath_dps</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/decorator.py#L79-L93"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.decorator.conserve_mpmath_dps" title="Permalink to this definition">¶</a></dt>
<dd><p>After the function finishes, resets the value of mpmath.mp.dps to
the value it had before the function was run.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.decorator.doctest_depends_on">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.decorator.</span></span><span class="sig-name descname"><span class="pre">doctest_depends_on</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">exe</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">modules</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">disable_viewers</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">python_version</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/decorator.py#L127-L173"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.decorator.doctest_depends_on" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds metadata about the dependencies which need to be met for doctesting
the docstrings of the decorated objects.</p>
<p>exe should be a list of executables</p>
<p>modules should be a list of modules</p>
<p>disable_viewers should be a list of viewers for preview() to disable</p>
<p>python_version should be the minimum Python version required, as a tuple
(like (3, 0))</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.decorator.memoize_property">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.decorator.</span></span><span class="sig-name descname"><span class="pre">memoize_property</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">propfunc</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/decorator.py#L225-L240"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.decorator.memoize_property" title="Permalink to this definition">¶</a></dt>
<dd><p>Property decorator that caches the value of potentially expensive
<span class="math notranslate nohighlight">\(propfunc\)</span> after the first evaluation. The cached value is stored in
the corresponding property name with an attached underscore.</p>
</dd></dl>

<dl class="py class">
<dt class="sig sig-object py" id="sympy.utilities.decorator.no_attrs_in_subclass">
<em class="property"><span class="pre">class</span> </em><span class="sig-prename descclassname"><span class="pre">sympy.utilities.decorator.</span></span><span class="sig-name descname"><span class="pre">no_attrs_in_subclass</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">cls</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">f</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/decorator.py#L96-L124"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.decorator.no_attrs_in_subclass" title="Permalink to this definition">¶</a></dt>
<dd><p>Don’t ‘inherit’ certain attributes from a base class</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.decorator</span> <span class="kn">import</span> <span class="n">no_attrs_in_subclass</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">A</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="gp">... </span>    <span class="n">x</span> <span class="o">=</span> <span class="s1">&#39;test&#39;</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">A</span><span class="o">.</span><span class="n">x</span> <span class="o">=</span> <span class="n">no_attrs_in_subclass</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="n">A</span><span class="o">.</span><span class="n">x</span><span class="p">)</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">B</span><span class="p">(</span><span class="n">A</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">pass</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">A</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="go">True</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">hasattr</span><span class="p">(</span><span class="n">B</span><span class="p">,</span> <span class="s1">&#39;x&#39;</span><span class="p">)</span>
<span class="go">False</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.decorator.public">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.decorator.</span></span><span class="sig-name descname"><span class="pre">public</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">obj</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/decorator.py#L176-L222"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.decorator.public" title="Permalink to this definition">¶</a></dt>
<dd><p>Append <code class="docutils literal notranslate"><span class="pre">obj</span></code>’s name to global <code class="docutils literal notranslate"><span class="pre">__all__</span></code> variable (call site).</p>
<p>By using this decorator on functions or classes you achieve the same goal
as by filling <code class="docutils literal notranslate"><span class="pre">__all__</span></code> variables manually, you just don’t have to repeat
yourself (object’s name). You also know if object is public at definition
site, not at some random location (where <code class="docutils literal notranslate"><span class="pre">__all__</span></code> was set).</p>
<p>Note that in multiple decorator setup (in almost all cases) <code class="docutils literal notranslate"><span class="pre">&#64;public</span></code>
decorator must be applied before any other decorators, because it relies
on the pointer to object’s global namespace. If you apply other decorators
first, <code class="docutils literal notranslate"><span class="pre">&#64;public</span></code> may end up modifying the wrong namespace.</p>
<p class="rubric">Examples</p>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">sympy.utilities.decorator</span> <span class="kn">import</span> <span class="n">public</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">__all__</span> <span class="c1"># noqa: F821</span>
<span class="gt">Traceback (most recent call last):</span>
<span class="c">...</span>
<span class="gr">NameError</span>: <span class="n">name &#39;__all__&#39; is not defined</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nd">@public</span>
<span class="gp">... </span><span class="k">def</span> <span class="nf">some_function</span><span class="p">():</span>
<span class="gp">... </span>    <span class="k">pass</span>
</pre></div>
</div>
<div class="doctest highlight-default notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="n">__all__</span> <span class="c1"># noqa: F821</span>
<span class="go">[&#39;some_function&#39;]</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.decorator.threaded">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.decorator.</span></span><span class="sig-name descname"><span class="pre">threaded</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/decorator.py#L39-L56"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.decorator.threaded" title="Permalink to this definition">¶</a></dt>
<dd><p>Apply <code class="docutils literal notranslate"><span class="pre">func</span></code> to sub–elements of an object, including <a class="reference internal" href="../core.html#sympy.core.add.Add" title="sympy.core.add.Add"><code class="xref py py-class docutils literal notranslate"><span class="pre">Add</span></code></a>.</p>
<p>This decorator is intended to make it uniformly possible to apply a
function to all elements of composite objects, e.g. matrices, lists, tuples
and other iterable containers, or just expressions.</p>
<p>This version of <a class="reference internal" href="#sympy.utilities.decorator.threaded" title="sympy.utilities.decorator.threaded"><code class="xref py py-func docutils literal notranslate"><span class="pre">threaded()</span></code></a> decorator allows threading over
elements of <a class="reference internal" href="../core.html#sympy.core.add.Add" title="sympy.core.add.Add"><code class="xref py py-class docutils literal notranslate"><span class="pre">Add</span></code></a> class. If this behavior is not desirable
use <a class="reference internal" href="#sympy.utilities.decorator.xthreaded" title="sympy.utilities.decorator.xthreaded"><code class="xref py py-func docutils literal notranslate"><span class="pre">xthreaded()</span></code></a> decorator.</p>
<p>Functions using this decorator must have the following signature:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@threaded</span>
<span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
</pre></div>
</div>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.decorator.threaded_factory">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.decorator.</span></span><span class="sig-name descname"><span class="pre">threaded_factory</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">use_add</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/decorator.py#L11-L36"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.decorator.threaded_factory" title="Permalink to this definition">¶</a></dt>
<dd><p>A factory for <code class="docutils literal notranslate"><span class="pre">threaded</span></code> decorators.</p>
</dd></dl>

<dl class="py function">
<dt class="sig sig-object py" id="sympy.utilities.decorator.xthreaded">
<span class="sig-prename descclassname"><span class="pre">sympy.utilities.decorator.</span></span><span class="sig-name descname"><span class="pre">xthreaded</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">func</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/sympy/sympy/blob/00d6469eafdd4aac346a0b598184c15f2560dbe5/sympy/utilities/decorator.py#L59-L76"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#sympy.utilities.decorator.xthreaded" title="Permalink to this definition">¶</a></dt>
<dd><p>Apply <code class="docutils literal notranslate"><span class="pre">func</span></code> to sub–elements of an object, excluding <a class="reference internal" href="../core.html#sympy.core.add.Add" title="sympy.core.add.Add"><code class="xref py py-class docutils literal notranslate"><span class="pre">Add</span></code></a>.</p>
<p>This decorator is intended to make it uniformly possible to apply a
function to all elements of composite objects, e.g. matrices, lists, tuples
and other iterable containers, or just expressions.</p>
<p>This version of <a class="reference internal" href="#sympy.utilities.decorator.threaded" title="sympy.utilities.decorator.threaded"><code class="xref py py-func docutils literal notranslate"><span class="pre">threaded()</span></code></a> decorator disallows threading over
elements of <a class="reference internal" href="../core.html#sympy.core.add.Add" title="sympy.core.add.Add"><code class="xref py py-class docutils literal notranslate"><span class="pre">Add</span></code></a> class. If this behavior is not desirable
use <a class="reference internal" href="#sympy.utilities.decorator.threaded" title="sympy.utilities.decorator.threaded"><code class="xref py py-func docutils literal notranslate"><span class="pre">threaded()</span></code></a> decorator.</p>
<p>Functions using this decorator must have the following signature:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nd">@xthreaded</span>
<span class="k">def</span> <span class="nf">function</span><span class="p">(</span><span class="n">expr</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
</pre></div>
</div>
</dd></dl>

</section>


            <div class="clearer"></div>
          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
            <p class="logo"><a href="../../index.html">
              <img class="logo" src="../../_static/sympylogo.png" alt="Logo"/>
            </a></p>
  <h4>Previous topic</h4>
  <p class="topless"><a href="codegen.html"
                        title="previous chapter">Codegen</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="enumerative.html"
                        title="next chapter">Enumerative</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../../_sources/modules/utilities/decorator.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3 id="searchlabel">Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="https://docs.sympy.org/latest/search.html" method="get">
      <input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script>$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="enumerative.html" title="Enumerative"
             >next</a> |</li>
        <li class="right" >
          <a href="codegen.html" title="Codegen"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="../../index.html">SymPy 1.9 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="../index.html" >SymPy Modules Reference</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="index.html" >Utilities</a> &#187;</li>
        <li class="nav-item nav-item-this"><a href="#">Decorator</a></li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2021 SymPy Development Team.
      Last updated on Sep 30, 2021.
      Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 4.1.2.
    </div>
  </body>

<!-- Mirrored from docs.sympy.org/latest/modules/utilities/decorator.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 03:28:12 GMT -->
</html>